package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016101800713318";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String  merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQClV4SK2xLQkrh33AZTEErSP0CvHX9Tiq77BArMmTgWAsx7ac1SyEvV/q3bqoMaL6YUy3CKfmDxJadH4UlnWKCcKCmMe5LlJh6spYhqHgiYxhm1H83D2HI9VdAEh7B3FWWFo/CA7O+G1sDZXaRmsIw/Y7R/XVh6cm66L//CFHpoKRSsoLYJizX2pda96ww2bK0H/L0A3RqWRTBdbNFqUvd1AETsdlaClEyDtcUjAOiIv+Yr1WX0SGhyT39D5tW+tS7x1CngiuQZwlltFc0xFVUSyoDQmoiMAPxQIpdrzREOIpHdOW997rWPrSK6nXdZKSo2A3vhbSKLGAewmhmfdDxDAgMBAAECggEAFS1Scp6AmGpSajeqL7Vkf+1OEZ/LkR2wL8Jix7XQMzGEk0IH5FR3Mh9+kSYTyKTVttAfx/1L2VcjR9eZGT2GxLl4KOAi/mqVfzNsd3sFStwEqug56J2udCCrlgjzFy2MZUOMKFcwjqqu5KhTyDtQm0Xf9ng0PuqtN/ex4F/TFcgIOz+v4fARVD3qQo74OmsuC9Rvl6CTPL0YAwA+4CHkqRfN4Ko5Dxy/vjVow9ufq3msCgLpoWgixsxYQleY22GydV6SlhwmK+vRF8ZHiz9jLIAkUOyYJsZRu4kehzjhvN66hBsuJX0UXxGW1RE+6nuWLVLz48bDZDdxj683/TIWYQKBgQDkP02mEI81hDOevIOUxaGkgJf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iHl8JrPMvQpJDQR+L4aek6SgfNZg62BhjaK4FOGY3AfHpSdn1eOzoCCb0zkI2aSWgFoDkFathXLhioI+MHAoGBAI42xiQDPxcFHl7J2moMyOS5Vz4a2Z0ntVFRHPV8va9CHRkX9yNJS75pKoMeNfq9kZyIqktzSeYj5/T8WCD6awUzO7+M/1239+T6MsdjNbPUlyAzzj9MjjVwyPsO4VxQoSq4JMuEbdFwdno55ZsxDN674IG/xCGjaralHTqQ/py3";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn0RxRogCci82PJYK/WaGBy9cWMzxb+yhdTmHLjcLJ8wHFSWsluJAxHI5wdgqJ4apqQoSlu6RHgnwy7QKCjboLM40ZhpyupLISUnhn95UHZf9PZrTDUaATDkieUiiOw+/33uxxsAXj9LFrz+4OW6GSfnnAD3pzRxSBUPoJ1UK+uT8zcwsMPaztH8HwUyuICpoScXuj7R8a7kpqCp2hcNBG3GZNn5F2PlAZOAN8mcV0jjzv+HdLSsCPN25L+6EFcm8mWS+QhmXttEpkzeChitSuuLQi2xaHXUAa75wttIqABM2XGZ5Qbxmgmuq7U9wi9Qu1szCuR+vBa97yNHIbeWY1wIDAQAB";
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //只要支付支付成功了，会每隔几秒发送一条消息，告知支付成功了，可以接收到这个消息以后，后台修改一下订单的这个成功页
    public static String notify_url = "http://182.61.1.143:9010/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //支付宝支付成功以后要跳转的页面地址
    public static String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "2m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
